package wre.logic;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.QueryTermExtractor;
import org.apache.lucene.search.highlight.WeightedTerm;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.wordnet.SynExpand;

/**
 * The Class InputParser takes in a string and breaks each string into individual words
 * and returns a list of the separated words as strings. 
 * 
 *  @author Adam Smith
 */
public class InputParser {
	
	/**
	 * Gets the single instance of InputParser.
	 *
	 * @return single instance of InputParser
	 */
	public static InputParser getInstance() {
		InputParser inputParser = new InputParser();
		return inputParser;

	}

	/** The searcher. */
	IndexSearcher searcher;

	/**
	 * Instantiates a new input parser.
	 */
	private InputParser() {

		try {
			searcher = new IndexSearcher(FSDirectory.open(new File("index")));

		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * Parses the input into separate words based on punctuation and spacing. 
	 * As the words are split up the words are placed into a list and the list
	 * of words is returned.
	 *
	 * @param input the input
	 * @return the list
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public List<String> parseInput(String input) throws IOException {
		Query query = SynExpand.expand(input, searcher, null, null, 0f);
		WeightedTerm[] terms = QueryTermExtractor.getTerms(query);
		ArrayList<String> words = new ArrayList<String>();
		for (WeightedTerm term : terms) {
			words.add(term.getTerm());
		}
		// Set<String> set = new HashSet<String>(words);
		// ArrayList<String> uniqueList = new ArrayList<String>(set);
		return words;

	}
}
